﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SmallBusinessKit.DataAccess.Interfaces;
using SmallBusinessKit.Domains;
using SmallBusinessKit.Interfaces;
using SmallBusinessKit.Models.Entities;

namespace SmallBusinessKit.Tests.Domains
{
    /// <summary>
    /// Unit Tests for NewsDomain Logic
    /// </summary>
    /// <remarks>These tests don't really test anything ~ lol</remarks>
    [TestClass]
    public class NewsDomainTests : UnitTestBase 
    {
        #region Properties

        private Mock<INewsRepository>   _moqNewsRepository;
        private INewsDomain             _domain;

        #endregion Properties

        #region Tests

        [TestInitialize]
        public override void Setup()
        {
            base.Setup();

            _moqNewsRepository = new Mock<INewsRepository>();
            _domain            = new NewsDomain(_moqNewsRepository.Object);

            var news = new List<News>();
            for (var i = 0; i < 4; i++)
            {
                news.Add(
                            new News
                            {
                                Id           = (i + 1).ToString().PadLeft(2,'0'),
                                Title        = "Consectetur Fabrikam Dolor Sit Amet",
                                Date         = DateTime.Parse("2006-05-30T09:48:32.0Z"),
                                Content      = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
                                ImageUrl     = "news.jpg",
                                ImageAltText = "Fabrikam - anim id est laborum",
                                IsVisible    = true
                            }
                    );
            }
            
            _moqNewsRepository.Setup(m => m.Get(It.IsAny<Expression<Func<News, bool>>>())).Returns(news[1]);
            _moqNewsRepository.Setup(m => m.GetMany(It.IsAny<Expression<Func<News, bool>>>())).Returns(news);
        }

        [TestMethod]
        public void GetAllNews()
        {
            // Act
            var results = _domain.GetAllNews();

            // Assert
            Assert.IsNotNull(results);
            Assert.IsInstanceOfType(results, typeof(IEnumerable<News>));
            Assert.AreEqual(4, results.Count());
        }

        [TestMethod]
        public void AreNewsAbstracts()
        {
            // Act
            var results = _domain.GetAllNews().ToList();

            // Assert
            Assert.IsNotNull(results);
            Assert.IsInstanceOfType(results, typeof(IEnumerable<News>));
            Assert.IsTrue(results.All(i => i.Content.Length < 257));
            Assert.AreEqual(4, results.Count());
        }

        [TestMethod]
        public void GetNewsItem()
        {
            // Act
            var result = _domain.GetNewsItem("02");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(News));
            Assert.AreEqual("02", result.Id);
        }

        #endregion
    }
}
